﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using My.DataAccess.Core;
using NCLCustomerManagement.DataTransferObjects.History;

namespace NCLCustomerManagement.DataAccess.NSHistory
{
    public class HistoryRepository : BaseRepository<HistoryDTO, History>, IHistoryRepository
    {

        public IEnumerable<HistoryDTO> GetHistoryListBy(HistoryType type)
        {
            return UnitOfWork.Table<History>()
                .Where(e => e.Type.Value == (int)type)
                .OrderByDescending(e => e.OperationDate)
                .Select(e => this.ConvertEntityToDTO(e))
                .AsEnumerable();
        }


        protected override HistoryDTO ConvertEntityToDTO(History entity)
        {
            var dto = base.ConvertEntityToDTO(entity);
            dto.Type = (HistoryType)entity.Type.Value;
            return dto;
        }

        protected override History ConvertDTOToEntity(HistoryDTO dto)
        {
            var entity = base.ConvertDTOToEntity(dto);            
            entity.Type = (int) dto.Type;
            return entity;
        }


        public void AddHistory(HistoryDTO dto)
        {
            UnitOfWork.InsertOnSubmit<History>(ConvertDTOToEntity(dto));
            UnitOfWork.SubmitChanges();
        }
    }
}
